package com.primb.bench.common.module.repository;

import android.text.TextUtils;

import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.entity.EnsureObjectEntity;
import com.primb.bench.project.home.entity.HttpEnsureMoneyEntity;
import com.primb.bench.project.home.entity.HttpEnsureObjectEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.RequestObjectEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Chen on 2017/8/4.
 * 功能描述：
 */

public class LoanEnsureRepository {

    public static Observable<List<EnsureObjectEntity>> getEnsureObjectListEntity(RequestObjectEntity entity) {
        HashMap<String, String> map = new HashMap<>();
        map.put("start", entity.getStart() + "");
        map.put("limit", "100");

        if (entity.getPriceId() != null && !TextUtils.isEmpty(entity.getPriceId())) {
            map.put("priceId", entity.getPriceId());
        }
        return ApiClient.initService(ApiService.class).getEnsureObjectList(map).flatMap(new Func1<HttpEnsureObjectEntity, Observable<List<EnsureObjectEntity>>>() {
            @Override
            public Observable<List<EnsureObjectEntity>> call(HttpEnsureObjectEntity httpEnsureObjectEntity) {
                return Observable.just(httpEnsureObjectEntity.getData());
            }
        }).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }


    public static Observable<HttpEnsureMoneyEntity.DataBean> saveObjectList(String priceId) {
        Observable<HttpEnsureMoneyEntity.DataBean> msgBeanObservable = ApiClient.initService(ApiService.class).saveObjectList(priceId).flatMap(new Func1<HttpEnsureMoneyEntity, Observable<HttpEnsureMoneyEntity.DataBean>>() {
            @Override
            public Observable<HttpEnsureMoneyEntity.DataBean> call(HttpEnsureMoneyEntity httpEnsureMoneyEntity) {
                if (!httpEnsureMoneyEntity.isSuccess()) {
                    Observable.error(new ApiException(httpEnsureMoneyEntity.getMsg()));
                }
                return Observable.just(httpEnsureMoneyEntity.getData());
            }
        });

//        Observable<HttpEnsureMoneyEntity.DataBean> msgBeanObservabler = ApiClient.initService(ApiService.class).saveObjectListr(priceId).flatMap(new Func1<HttpEnsureMoneyEntity, Observable<HttpEnsureMoneyEntity.DataBean>>() {
//            @Override
//            public Observable<HttpEnsureMoneyEntity.DataBean> call(HttpEnsureMoneyEntity httpEnsureMoneyEntity) {
//                if (!httpEnsureMoneyEntity.isSuccess()) {
//                    Observable.error(new ApiException(httpEnsureMoneyEntity.getMsg()));
//                }
//                return Observable.just(httpEnsureMoneyEntity.getDataBean());
//            }
//        });
        return msgBeanObservable.compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static Observable<HttpStrEntity> deleteEnsureObject(String guatantId) {
        return ApiClient.initService(ApiService.class).deleteEnsureObject(guatantId).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }
}
